using System;
using System.Collections.Generic;
using System.Text;
using LibGreen.Jabber.Enumerations;
using System.Collections.ObjectModel;

namespace LibGreen.Jabber
{
    /// <summary>
    /// Represents an item from the client's roster
    /// </summary>
    public class RosterItem : IComparable
    {
        private readonly Jid jid;
        private string name;
        private Subscription subscription;
        private readonly List<RosterGroup> groups = new List<RosterGroup>();

        /// <summary>
        /// Initializes a new RosterItem
        /// </summary>
        /// <param name="jid">The <see cref="Jid"/> of this roster item</param>
        public RosterItem(Jid jid)
        {
            this.jid = jid;
        }

        /// <summary>
        /// Gets the <see cref="Jid"/> of the item
        /// </summary>
        public Jid JID
        {
            get { return jid; }
        }

        /// <summary>
        /// Gets the display name of the item
        /// </summary>
        public string Name
        {
            get { return name; }
            internal set { name = value; }
        }

        /// <summary>
        /// Gets the <see cref="Subscription"/> type of this item
        /// </summary>
        public Subscription Subscription
        {
            get { return subscription; }
            internal set { subscription = value; }
        }

        /// <summary>
        /// Gets a list of groups to which this item belongs
        /// </summary>
        public ReadOnlyCollection<RosterGroup> Groups
        {
            get { return new ReadOnlyCollection<RosterGroup>(groups); }
        }

        /// <summary>
        /// Adds a new group to the list of groups to which this item belongs
        /// </summary>
        /// <param name="group">The group to add</param>
        internal void AddGroup(RosterGroup group)
        {
            if (!groups.Contains(group))
            {
                groups.Add(group);
            }
        }

        #region IComparable Members
        /// <summary>
        /// Compares the current instance with another object of the same type.
        /// </summary>
        /// <param name="obj">An object to compare with this instance.</param>
        /// <returns>A 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
        public int CompareTo(object obj)
        {
            RosterItem other = obj as RosterItem;
            if (other != null)
            {
                return JID.CompareTo(other.JID);
            }
            return -1;
        }

        /// <summary>
        /// Determines whether the specified Object is equal to the current Object.
        /// </summary>
        /// <param name="obj">The Object to compare with the current Object.</param>
        /// <returns>true if the specified Object is equal to the current Object; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            return CompareTo(obj) == 0;
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>A hash code for the current object</returns>
        public override int GetHashCode()
        {
            return JID.GetHashCode();
        }
        #endregion
    }
}
